import { useState, useEffect } from 'react';

export const useDropdownPosition = () => {
  const [featuresDropdownUp, setFeaturesDropdownUp] = useState(true);
  const [businessDropdownUp, setBusinessDropdownUp] = useState(true);
  const [atmosphereDropdownUp, setAtmosphereDropdownUp] = useState(true);

  // 检测下拉框位置，决定是否向上展开
  const checkDropdownPosition = (buttonRef: React.RefObject<HTMLDivElement | null>, setDropdownUp: (up: boolean) => void) => {
    if (!buttonRef.current) return;
    
    const buttonRect = buttonRef.current.getBoundingClientRect();
    const viewportHeight = window.innerHeight;
    const dropdownHeight = 200; // 预估下拉框高度
    
    // 如果按钮下方空间不够，则向上展开
    const spaceBelow = viewportHeight - buttonRect.bottom;
    const shouldOpenUp = spaceBelow < dropdownHeight;
    
    setDropdownUp(shouldOpenUp);
  };

  // 监听窗口大小变化，重新计算下拉框位置
  useEffect(() => {
    const handleResize = () => {
      // 这里可以根据需要重新计算位置
    };

    window.addEventListener('resize', handleResize);
    return () => {
      window.removeEventListener('resize', handleResize);
    };
  }, []);

  return {
    featuresDropdownUp,
    setFeaturesDropdownUp,
    businessDropdownUp,
    setBusinessDropdownUp,
    atmosphereDropdownUp,
    setAtmosphereDropdownUp,
    checkDropdownPosition
  };
};
